$(function() {
    BUI.use([ 'bui/form','common/search','bui/grid', 'bui/data','bui/overlay','bui/uploader' ], function(Form,Search,Grid,Data, Overlay,Uploader) {
        var Grid = Grid,
            Store = Data.Store,

            columns = [
                {
                    title: '美容师ID',
                    dataIndex: 'id',
                    width: 30
                },
                {
                    title: '美容师名称',
                    dataIndex: 'name',
                    width: 60
                },
                {
                    title: '性别',
                    dataIndex: 'six',
                    width: 30
                },
                {
                    title: '城市',
                    dataIndex: 'city',
                    width: 40
                },
                {
                    title: '地址',
                    dataIndex: 'address',
                    width: 90
                },
                {
                    title: '描述',
                    dataIndex: 'comment',
                    width: 140
                },
                {
                    title: '创建时间',
                    dataIndex: 'create_time',
                    width: 80
                }];

        var editing = new Grid.Plugins.DialogEditing({
            contentId : 'content',
            triggerCls : 'btn-edit',
            editor : {
                title : '添加',
                width : 780,
                buttons:[
                    {
                        text:'保存',
                        elCls : 'button button-primary',
                        handler : function(){
                            var vidd = $('#id').val();
                            if(vidd=='undefined' || vidd=='' || null==vidd ){
                                form.set('action','/tech/add/');
                            }else{
                                alert(data.status.msg);
                            }
                            form.submit();
                            this.close();
                        }
                    }
                    ,{
                        text:'关闭',
                        elCls : 'button',
                        handler : function(){
                            this.close();
                        }
                    }
                ]
            }
        });
        editing.on('editorshow', function(ev){
            var editType = editing.get('editType');//add 或者 edit
            if(editType == 'add'){
                ev.eidtor.set('title', '添加');
            }else{
                ev.eidtor.set('title', '编辑');
            }
            select.setSelectedValue('');
            if(ev.editType=='add'){
                select.setSelectedValue('mongodb');
            }else{
                select.setSelectedValue($("#dbType").val());
            }
        });

        var  store = new Data.Store({
                url : '/tech/find',//chaxun
                proxy: {
                    limitParam: 'pageSize',
                    pageIndexParam: 'pageNumber',
                    pageStart: 1
                },
                autoLoad: false,
                pageSize: 20,
                errorProperty: 'message',
                hasErrorProperty: 'code',
                root: 'data',
                totalProperty: 'totalPage'
            }),

            grid = new Grid.Grid({
                render:'#grid',
                forceFit: true,	// 列宽按百分比自适应
                columns : columns,
                store : store,
                tbar:{ //添加、删除
                    items : [{
                        btnCls : 'button button-small',
                        text : '<i class="icon-plus"></i>添加',
                        listeners : {
                            'click' : addFunction
                        }
                    } ,{
                        btnCls : 'button button-small',
                        text : '<i class="icon-remove"></i>删除',
                        listeners : {
                            'click' : delFunction
                        }
                    }
                    ]
                },
                bbar:{
                    pagingBar:true
                },
                //plugins : [editing,Grid.Plugins.CheckSelection]
                plugins : [editing,Grid.Plugins.RadioSelection]	// 插件形式引入单选表格
            });

        grid.render();
        var search =  new Search({
            store : store,
            grid : grid
        });

        var form = new Form.HForm({ //创建表单
            srcNode : '#J_Form',
            submitType : 'normal',
            type: 'POST',
            action : '/tech/add/',
            enctype:'multipart/form-data',
            callback : function(data) {
                if (data.code == 0) {
                    store.load();
                }else{
                    alert(data.status.msg);
                }
            }
        }).render();

        function addFunction(){
            editing.add();
        }

//        删除多选项
        function delFunction(){
            var selections = grid.getSelection();
            delItems(selections);
        }
        function delItems(items){
            var ids = [];
            BUI.each(items,function(item){
                ids.push(item.id);
            });

            if(ids.length){
                var idList = ids.join(",");
                BUI.Message.Confirm('确认要删除选中的记录么？',function(){
                    $.ajax({
                        type: 'GET',
                        url : '/tech/del',
                        dataType : 'json',
                        data : {id:idList },
                        success : function(data){
                            if(data.code == 0){ //删除成功
                                BUI.Message.Alert('删除成功!','success');
                                search.load();
                            }else{//删除失败
                                BUI.Message.Alert(data.status.msg,'error');
                            }
                        }
                    });
                },'question');
            }else{
                BUI.Message.Alert('请至少选择一条删除记录!','info');
            }
        }
    });
    BUI.use('bui/form',function(Form){
        Form.Rules.add({
            name : 'sid',  //规则名称
            msg : '请输入有效数字',//默认显示的错误信息
            validator : function(value,baseValue,formatMsg){ //验证函数，验证值、基准值、格式化后的错误信息
                var regexp = new RegExp('^\\d+$');
                if(value && !regexp.test(value)){
                    return formatMsg;
                }
            }
        });
    });
});